/******************************************************************************
January 19, 2017
Sui-Jade Ho
Dimitrije Ruzic

Input(s): 
This .do file has 2 sections.
SECTION 1:
	- 
	
******************************************************************************/

clear programs
clear all
set more off
set matsize 11000

local data "/FILE PATH GOES HERE/"
local output "/FILE PATH GOES HERE/"
local boots "/FILE PATH GOES HERE/"

/*SECTION 1: Establishment-level TFPQ and TAU, and Industry-level Correlations*/
local S1 = 0
/*SECTION 2: Overview of US Manufacturing*/
local S2 = 0
/*SECTION 3: Aggregate and Industry-level Results*/
local S3 = 0
/*SECTION 4: Establishment Regressions*/
local S4 = 0
/*SECTION 5: Decomposing the Bias*/
local S5 = 0

/*********************************************************
SECTION 1: 
	- Construct TFPQ, TAU, and K/L for different models
	- Construct correlations of productivity and distortions
		
**********************************************************/
if `S1' == 1 {

!gunzip /FILE PATH GOES HERE/Manufacturing_1977_2009.dta.gz
use "`data'Manufacturing_1977_2009.dta", clear
*!gzip /FILE PATH GOES HERE/Manufacturing_1977_2009.dta

keep et fk_naics02 year lbdnum PY2 Y2_* L K sw R wt
drop if year>2007

/*Keep ASM Sample Only*/
keep if et==0
drop if wt<0

/*Merge in sample restriction*/
merge 1:1 lbdnum year fk_naics02 using "`data'Misallocation_Sample_vrts_1acf_sest_5y.dta"
drop _m

gen period = 1 if year<=1982
	replace period = 2 if year>1982 & year<=1987
	replace period = 3 if year>1987 & year<=1992
	replace period = 4 if year>1992 & year<=1997
	replace period = 5 if year>1997 & year<=2002
	replace period = 6 if year>2002 & year<=2007
	
	
gen dec = 1 if year<=1987
	replace dec = 2 if year>1987 & year<=1997
	replace dec = 3 if year>1997 & year<=2007

/*Merge in production-function parameters*/
merge m:1 fk period dec using "`data'PF_FK_ACF.dta"
drop if _m==2
drop _m

/*VRTS, SEST*/
gen alphaL_m1 = aL_1acf_vrts_5y
gen alphaK_m1 = aK_1acf_vrts_5y
gen sigma_m1 = sigma_1acf_vrts_5y

/*CRTS, SEST*/
gen alphaL_m2 = aL_1acf_crts_5y
gen alphaK_m2 = aK_1acf_crts_5y
gen sigma_m2 = sigma_1acf_vrts_5y

/*CRTS, S3*/
gen alphaL_m3 = aL_1acf_vrts_5y
gen alphaK_m3 = aK_1acf_vrts_5y
gen sigma_m3 = sigma_3

/*CRTS, SIMP*/
gen alphaL_m4 = aL_1acf_vrts_5y
gen alphaK_m4 = aK_1acf_vrts_5y
gen sigma_m4 = sigma_1acf_crts_5y

drop *_1acf_* *_2acf_* *_hk_*

	foreach m of numlist 1 2 3 4 {
	*foreach m of numlist 1 2 3 4 5 {
		gen alphaL = alphaL_m`m'
		gen alphaK = alphaK_m`m'
		gen sigma = sigma_m`m'
		gen double Y2 = (PY2)^(sigma/(sigma-1))

		/*Calculate physical productivity, TFPQ*/
		gen double tfpq_ASM = Y2/((L^alphaL)*(K^alphaK))
			replace tfpq_ASM = . if sample_ASM!=1
			
		/*Calculate relative distortions*/
		local dtypes "ASM"
		sort fk_naics02 year
		foreach d of local dtypes {
			if "`d'"=="ASM" {
				gen temp = PY2*wt if sample_`d'==1
				by fk_naics02 year: egen py_weight_total = sum(temp) if sample_`d'==1
				gen py_weight = (temp)/py_weight_total
				drop temp py_weight_total
				
				gen temp = (K/(PY2)) if sample_`d'==1
				gen temp2 = temp*py_weight
				by fk_naics02 year: egen tauk_bar = sum(temp2)
				gen tauk_`d' = tauk_bar/temp
				drop tauk_bar temp temp2
				
				gen temp = (L/(PY2)) if sample_`d'==1
				gen temp2 = temp*py_weight
				by fk_naics02 year: egen taul_bar = sum(temp2)
				gen taul_`d' = taul_bar/temp
				drop taul_bar temp temp2 py_weight
				
				}
			}
				
		foreach var of varlist tfpq_ASM tauk_ASM taul_ASM {
			replace `var' = . if alphaL==. | alphaK==.
			}
		
		rename tfpq_ASM tfpq_ASM_m`m'
		
		rename tauk_ASM tauk_ASM_m`m'
		
		rename taul_ASM taul_ASM_m`m'
		
		drop alphaK alphaL sigma Y2
		}

keep if sample_ASM==1

local types "ASM"
foreach d of local types {
	if "`d'"=="ASM" {
		gen temp = PY2*wt if sample_`d'==1
		by fk_naics02 year: egen py_weight_total = sum(temp) if sample_`d'==1
		gen py_weight_ASM = (temp)/py_weight_total
		drop temp
		
		gen wt_`d' = wt if sample_`d'==1
		
		by fk_naics02 year: egen t`d' = sum(wt_`d')
		}
	}
sort fk year
foreach m of numlist 1/4 {

	gen input_m`m' = K^alphaK_m`m'*L^alphaL_m`m'
	gen rts_m`m' = alphaK_m`m'+alphaL_m`m'
	gen dmarkup_m`m' = sigma_m`m'/(sigma_m`m'-1)-sigma_m1/(sigma_m1-1)
	
	/*Combined Distortion*/
	gen tau_m`m' = tauk_ASM_m`m'^alphaK_m`m'*taul_ASM_m`m'^alphaL_m`m'
	
	/*Relative Productivity*/
	by fk year: egen tempmean = mean(tfpq_ASM_m`m')
	gen tempnorm = tfpq_ASM_m`m'/tempmean
	gen double temp = tempnorm^((sigma_m`m'-1)/(alphaK_m`m'+alphaL_m`m'+sigma_m`m'*(1-alphaK_m`m'-alphaL_m`m')))
	
		by fk_naics02 year: egen double temp2 = sum(temp*wt)
		
	gen double tfpr2_ratio_cf = (temp2/temp)^(1-alphaK_m`m'-alphaL_m`m')
		drop temp temp2 tempmean tempnorm

	/*Counterfactual Industry TFP*/
	gen double temp = (tfpq_ASM_m`m'*tfpr2_ratio_cf)
	by fk year: egen double tempmean = mean(temp)
	gen double tempnorm = temp/tempmean
	gen double temp2 = wt*tempnorm^(sigma_m`m'-1)
	by fk_naics02 year: egen double temp3 = sum(temp2)
	gen double TFP3_cf_m`m' = temp3^(1/(sigma_m`m'-1))*tempmean
	drop temp temp2 temp3 tempmean tempnorm tfpr2_ratio_cf
	
	gen normTFP_m`m' = TFP3_cf_m`m'/(tASM^(sigma_m`m'/(sigma_m`m'-1)-alphaK_m`m'-alphaL_m`m'))
	
	gen rtfpq_ASM_m`m'=tfpq_ASM_m`m'/normTFP_m`m'
	
	/*Correlations*/
	local var1 "rtfpq_ASM_m`m'"
	local var2 "tau_m`m'"
	
	by fk_naics02 year: egen m_`var1' = sum(`var1'*py_weight_ASM)
	by fk_naics02 year: egen m_`var2' = sum(`var2'*py_weight_ASM)

	*gen temp = wt*(`var1'-m_`var1')*(`var2'-m_`var2')
	gen temp = py_weight_ASM*(`var1'-m_`var1')*(`var2'-m_`var2')
	bys fk_naics02 year: egen num = sum(temp)
	drop temp

	*gen temp = wt*(`var1'-m_`var1')^2
	gen temp = py_weight_ASM*(`var1'-m_`var1')^2
	bys fk_naics02 year: egen denom1 = sum(temp)
	replace denom1 = sqrt(denom1)
	drop temp

	*gen temp = wt*(`var2'-m_`var2')^2
	gen temp = py_weight_ASM*(`var2'-m_`var2')^2
	bys fk_naics02 year: egen denom2 = sum(temp)
	replace denom2 = sqrt(denom2)
	drop temp

	gen rho_m`m' = num/(denom1*denom2)
	drop num denom*
	
	}
	
	egen ind = group(fk)
	egen cells = group(fk year)
	
	foreach var of varlist input* PY2 tfpq*_m* tau*_m* rtfpq*_m* {
		gen l`var' = log(`var')
		}

compress
save "`data'Misallocation_Establishment_Micro.dta", replace

collapse (mean) rho* alphaL* alphaK* sigma* py_weight_total, by(fk year)

save "`data'Misallocation_Correlations_industry.dta", replace


}
/*********************************************************
SECTION 2: 
	- Compiles measures of RTS and Markups for 
		ASM Manufacturing
		
**********************************************************/
if `S2' == 1 {
/*Baseline*/
use "`data'VA_Shares_fk.dta", clear

gen period = 1 if year<=1982
	replace period = 2 if year>1982 & year<=1987
	replace period = 3 if year>1987 & year<=1992
	replace period = 4 if year>1992 & year<=1997
	replace period = 5 if year>1997 & year<=2002
	replace period = 6 if year>2002 & year<=2007
	
sort fk year
merge m:1 fk period using "`data'PF_FK_ACF.dta"
drop _m

bys year: egen va_total = sum(va_fk)
gen py_share = va_fk/va_total

egen ty = tag(year) 

sort year
local types "5y 10y base"
foreach type of local types {
	foreach num of numlist 1/2 {	
		gen temp = rts_`num'acf_`type'*py_share
		by year: egen rts_y`num'_`type'=sum(temp)
		drop temp
		gen temp = py_share*(rts_`num'acf_`type'-rts_y`num'_`type')^2
		bys year: egen temp2 = sum(temp)
		gen sd_rts_y`num'_`type' = (temp2)^(0.5)
		drop temp temp2
		
		gen temp = mu_`num'acf_vrts_`type'*py_share
		by year: egen mu_y`num'_`type'=sum(temp)
		drop temp
		gen temp = py_share*(mu_`num'acf_vrts_`type'-mu_y`num'_`type')^2
		bys year: egen temp2 = sum(temp)
		gen sd_mu_y`num'_`type' = (temp2)^(0.5)
		drop temp temp2
		
		gen temp = aL_`num'acf_vrts_`type'*py_share/mu_`num'acf_vrts_`type'
		by year: egen betaL_y`num'_`type'=sum(temp)
		drop temp
		gen temp = py_share*(aL_`num'acf_vrts_`type'/mu_`num'acf_vrts_`type'-betaL_y`num'_`type')^2
		bys year: egen temp2 = sum(temp)
		gen sd_betaL_y`num'_`type' = (temp2)^(0.5)
		drop temp temp2
		
		gen temp = aK_`num'acf_vrts_`type'*py_share/mu_`num'acf_vrts_`type'
		by year: egen betaK_y`num'_`type'=sum(temp)
		drop temp
		gen temp = py_share*(aK_`num'acf_vrts_`type'/mu_`num'acf_vrts_`type'-betaK_y`num'_`type')^2
		bys year: egen temp2 = sum(temp)
		gen sd_betaK_y`num'_`type' = (temp2)^(0.5)
		drop temp temp2
		
		gen beta = (aL_`num'acf_vrts_`type'+aK_`num'acf_vrts_`type')/mu_`num'acf_vrts_`type'
		gen temp = beta*py_share
		by year: egen beta_y`num'_`type'=sum(temp)
		drop temp
		
		gen pi = 1-beta
		gen temp = pi*py_share
		by year: egen pi_y`num'_`type'=sum(temp)
		drop temp beta
		gen temp = py_share*(pi-pi_y`num'_`type')^2
		bys year: egen temp2 = sum(temp)
		gen sd_pi_y`num'_`type' = (temp2)^(0.5)
		drop temp temp2 pi
		
		}
		
	}

local num = "1"
local type = "5y"
*br year rts_y`num'_`type' mu_y`num'_`type' betaL_y`num'_`type' betaK_y`num'_`type' pi_y`num'_`type' if ty==1 & (year==1977 | year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
		
putexcel A1= ("Table 1: Overview of US Manufacturing, 1977-2007") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

putexcel A3= ("Panel A: Division of Value Added") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A4= ("Average") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A5= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel B5= ("Labor Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel C5= ("Capital Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel D5= ("Profit Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

putexcel F4= ("Standard Deviation") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel F5= ("Labor Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel G5= ("Capital Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel H5= ("Profit Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

putexcel A13= ("Panel B: Returns to Scale and Markups") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A14= ("Average") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A15= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel B15= ("Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel C15= ("Markup") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

putexcel F14= ("Standard Deviation") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel F15= ("Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel G15= ("Markup") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

putexcel A23= ("Panel C: Change in Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A24= ("5-Year Growth Rates") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A25= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel B25= ("Aggregate RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel C25= ("Industry RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel D25= ("Industry VA Shares") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

preserve
keep if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
local num = "1"
local type = "5y"
keep year *rts_y`num'_`type' *mu_y`num'_`type' *betaL_y`num'_`type' *betaK_y`num'_`type' *pi_y`num'_`type'

foreach var of varlist year *rts_y`num'_`type' *mu_y`num'_`type' *betaL_y`num'_`type' *betaK_y`num'_`type' *pi_y`num'_`type' {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}
	
mkmat year betaL_y`num'_`type' betaK_y`num'_`type' pi_y`num'_`type', mat(m1a)
mkmat sd_betaL_y`num'_`type' sd_betaK_y`num'_`type' sd_pi_y`num'_`type', mat(m1b)

mkmat year rts_y`num'_`type' mu_y`num'_`type', mat(m2a)
mkmat sd_rts_y`num'_`type' sd_mu_y`num'_`type', mat(m2b)

putexcel A6 = matrix(m1a) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel F6 = matrix(m1b) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel A16 = matrix(m2a) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
putexcel F16 = matrix(m2b) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify
restore

preserve
keep if (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
	
egen ind = group(fk)
tsset ind period

local dt = 1

foreach var of varlist rts_y1_5y rts_1acf_5y py_share {
	gen g_`var' = (`var'-l`dt'.`var')/l`dt'.`var'
	}

*gen weight = l`dt'.py_share*l`dt'.rts_1acf_5y/l`dt'.rts_y1_5y
gen weight = l`dt'.py_share*l`dt'.rts_1acf_5y/l`dt'.rts_y1_5y

foreach var of varlist rts_1acf_5y py_share {
	gen w_`var' = weight*g_`var'
	}
	
bys period: egen contrib_rts = sum(w_rts_1acf_5y)
bys period: egen contrib_py = sum(w_py_share)
gen test = contrib_rts+contrib_py

*br g_rts_y1_5y contrib* test if ty==1

keep if ty==1

foreach var of varlist g_rts_y1_5y contrib* {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}

mkmat year g_rts_y1_5y contrib*, mat(m3)
putexcel A26 = matrix(m3) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 1") modify

restore

putexcel A1= ("Table 6: Overview of US Manufacturing, 1977-2007, Alternative Models") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify

putexcel A3= ("Panel A: ACF Estimate of the Labor Revenue Elasticity") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel A4= ("Average") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel A5= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel B5= ("Labor Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel C5= ("Capital Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel D5= ("Profit Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel E5= ("Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel F5= ("Markup") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify

putexcel A13= ("Panel B: Fixed Returns to Scale and Markups Over Time") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel A14= ("Average") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel A15= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel B15= ("Labor Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel C15= ("Capital Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel D15= ("Profit Share") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel E15= ("Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
putexcel F15= ("Markup") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify

preserve
keep if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
local num = "2"
local type = "5y"
keep year *rts_y`num'_`type' *mu_y`num'_`type' *betaL_y`num'_`type' *betaK_y`num'_`type' *pi_y`num'_`type'

foreach var of varlist year *rts_y`num'_`type' *mu_y`num'_`type' *betaL_y`num'_`type' *betaK_y`num'_`type' *pi_y`num'_`type' {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}
	
mkmat year betaL_y`num'_`type' betaK_y`num'_`type' pi_y`num'_`type' rts_y`num'_`type' mu_y`num'_`type', mat(m6a)

putexcel A6 = matrix(m6a) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
restore

preserve
keep if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
local num = "1"
local type = "base"
keep year *rts_y`num'_`type' *mu_y`num'_`type' *betaL_y`num'_`type' *betaK_y`num'_`type' *pi_y`num'_`type'

foreach var of varlist year *rts_y`num'_`type' *mu_y`num'_`type' *betaL_y`num'_`type' *betaK_y`num'_`type' *pi_y`num'_`type' {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}
	
mkmat year betaL_y`num'_`type' betaK_y`num'_`type' pi_y`num'_`type' rts_y`num'_`type' mu_y`num'_`type', mat(m6b)

putexcel A16 = matrix(m6b) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 6") modify
restore

}

/*********************************************************
SECTION 3: 
	- Aggregate Measures of Misallocation
	- Robustness Checks
		
**********************************************************/
if `S3' == 1 {
/*Baseline + Robustness: CMF Sample*/
use "`data'Misallocation_Industry_CM_vrts_1acf_sest_5y.dta", clear
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_sest_5y.dta"
		drop _m
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_s3_5y.dta"
		drop _m
		
/*Robustness: Same profit margin, CRTS, implied markups*/
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_simp_5y.dta"
		drop _m
		
/*Robustness: Heterogeneous Markups*/
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_VM_vrts_1acf_sest_5y.dta"
		drop _m
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_VM_crts_1acf_s3_5y.dta"
		drop _m

/*Robustness: 10y*/
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_vrts_1acf_sest_10y.dta"
		drop _m
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_s3_10y.dta"
		drop _m
		
/*Robustness: ACF2*/
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_vrts_2acf_sest_5y.dta"
		drop _m
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_2acf_s3_5y.dta"
		drop _m
		
/*Robustness: 31-year period (base)*/
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_vrts_1acf_sest_base.dta"
		drop _m
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_s3_base.dta"
		drop _m
		
/*Robustness: FK4*/
gen naics4 = substr(fk,1,4)
	merge m:1 naics4 year using "`data'Misallocation_Industry_CM_vrts_1acf_sest_5y_4.dta"
		drop _m
	merge m:1 naics4 year using "`data'Misallocation_Industry_CM_crts_1acf_s3_5y_4.dta"
		drop _m

merge 1:1 fk_naics02 year using "`data'VA_Shares_fk.dta"		
	drop _m
	
foreach var of varlist M_i*5y M_i*10y M_i*base {
	gen temp = va_fk if `var'!=.
	bys year: egen temp_total = sum(temp)
	gen share = temp/temp_total
		drop temp temp_total
		
	gen temp = share*log(`var')
	by year: egen temp_total = sum(temp)
	
	local name = substr("`var'",4,.)
	
	gen tM`name' = exp(temp_total)
		drop temp temp_total share
	}

preserve
collapse (mean) M_i*4 (sum) va_fk, by(naics4 year)

foreach var of varlist M_i*4 {
	
	gen temp = va_fk if `var'!=.
	bys year: egen temp_total = sum(temp)
	gen share = temp/temp_total
		drop temp temp_total
		
	gen temp = share*log(`var')
	by year: egen temp_total = sum(temp)
	
	local name = substr("`var'",4,.)
	
	gen tM`name' = exp(temp_total)
		drop temp temp_total share
	}

keep naics4 year tM*
tempfile tM4
	save "`tM4'"
restore

merge m:1 naics4 year using "`tM4'"
	drop _m

/*Net Counterfactuals*/
foreach var of varlist tM* M* {
	replace `var' = `var'-1
	}

/*Output Aggregate Misallocation Time Series*/

egen ty = tag(year)
keep if ty==1
keep year M_ASM* M_CMF* tM_ASM* tM_CMF*
foreach var of varlist M* tM* {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}

*br year M_ASM* if (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007) 
*br year M_ASM*

/************
MAIN RESULTS
*************/
preserve
keep if (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007) 
putexcel A1= ("Table 2: Misallocation Under Different Models of the World") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel A3= ("Panel A: Misallocation") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel A5= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel B4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel B5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel C4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel C5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel D4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel D5= ("Sigma = 3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel F4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel F5= ("Implied Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

mkmat year tM_ASM_CM_vrts_1acf_sest_5y tM_ASM_CM_crts_1acf_sest_5y tM_ASM_CM_crts_1acf_s3_5y, mat(t2_1)
mkmat tM_ASM_CM_crts_1acf_simp_5y, mat(t2_2)

putexcel A6 = matrix(t2_1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel F6 = matrix(t2_2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

restore

preserve

use "`data'Misallocation_Correlations_industry.dta", clear
	
merge 1:1 fk year using "`data'VA_Shares_fk.dta"
	drop _m
	
bys year: egen total = sum(va_fk)
gen share = va_fk/total

foreach num of numlist 1/4 {
	by year: egen rho_y_m`num' = sum(rho_m`num'*share)
	}
	
egen ty = tag(year)
keep if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
keep year rho_y*

foreach var of varlist rho_y* {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}


putexcel A13= ("Panel B: Correlation of Productivity and Distortion") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel A15= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel B14= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel B15= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel C14= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel C15= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel D14= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel D15= ("Sigma = 3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

putexcel F14= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel F15= ("Implied Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

mkmat year rho_y_m1 rho_y_m2 rho_y_m3, mat(t2_3)
mkmat rho_y_m4, mat(t2_4)

putexcel A16 = matrix(t2_3) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify
putexcel F16 = matrix(t2_4) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 2") modify

restore



/************
ROBUSTNESS
*************/
keep if (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007) 

putexcel A1= ("Table 3: Aggregate Counterfactual Gains (Within Year), Robustness") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel A5= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify

putexcel B3= ("Robustness: Heterogeneous Markups") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel B4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel B5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel C4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel C5= ("Sigma=3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
mkmat year tM_ASM_VM_vrts_1acf_sest_5y tM_ASM_VM_crts_1acf_s3_5y, mat(t3_1)
putexcel A6 = matrix(t3_1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify

putexcel E3= ("Robustness: CMF Sample") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel E4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel E5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel F4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel F5= ("Sigma=3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
mkmat tM_CMF_CM_vrts_1acf_sest_5y tM_CMF_CM_crts_1acf_s3_5y, mat(t3_2)
putexcel E6 = matrix(t3_2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify

putexcel H3= ("Robustness: 10-year Estimation Sample") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel H4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel H5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel I4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel I5= ("Sigma=3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
mkmat tM_ASM_CM_vrts_1acf_sest_10y tM_ASM_CM_crts_1acf_s3_10y, mat(t3_3)
putexcel H6 = matrix(t3_3) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify

putexcel K3= ("Robustness: NAICS4 Instead of NAICS6") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel K4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel K5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel L4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel L5= ("Sigma=3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
mkmat tM_ASM_CM_vrts_1acf_sest_5y_4 tM_ASM_CM_crts_1acf_s3_5y_4, mat(t3_4)
putexcel K6 = matrix(t3_4) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify

putexcel N3= ("Robustness: ACF Labor Revenue Elasticity") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel N4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel N5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel O4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel O5= ("Sigma=3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
mkmat tM_ASM_CM_vrts_2acf_sest_5y tM_ASM_CM_crts_2acf_s3_5y, mat(t3_5)
putexcel N6 = matrix(t3_5) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify

putexcel Q3= ("Robustness: Fixed RTS and Markups Over Time") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel Q4= ("Estimated RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel Q5= ("Estimated Sigma") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel R4= ("Constant RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
putexcel R5= ("Sigma=3") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify
mkmat tM_ASM_CM_vrts_1acf_sest_base tM_ASM_CM_crts_1acf_s3_base, mat(t3_6)
putexcel Q6 = matrix(t3_6) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 3") modify




}




/*********************************************************
SECTION 4: 
	- Compare Biases Across Establishments
		
**********************************************************/
if `S4' == 1 {


!gunzip /rdcprojects/mc/mc01298/programs/HR/Data/Misallocation_Establishment_Micro.dta.gz
use "`data'Misallocation_Establishment_Micro.dta", clear

keep if sample_ASM==1
keep if (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)

replace wt = 1 if wt<0

gen input = (K^alphaK_m1)*(L^alphaL_m1)
gen linput = log(input)
gen lva = log(PY2)

/*Expressing Variables Relative to their standard deviations*/	
foreach var of varlist lrtfpq* ltfpq* linput lva {
	gen ltemp = `var'*py_weight_ASM
	by fk_naics02 year: egen ltemp_mean = sum(ltemp)
		
	gen ltemp_sqdv_wt = (`var'-ltemp_mean)^2*py_weight_ASM
	by fk_naics02 year: egen ltemp_sqdv_sum = sum(ltemp_sqdv_wt)
	gen ltemp_sd = sqrt(ltemp_sqdv_sum)
	gen n_`var' = (`var'-ltemp_mean)/ltemp_sd
	drop ltemp*
		}

/*Establishment Regressions: Normalized Variables*/
putexcel A1= ("Table 4: Productivity Mismeasurement at the Establishment Level") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A2= ("Panel A: Imposing Constant Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

putexcel A6= ("Normalized log Input Bundle") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A9= ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A10= ("(Variable RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

putexcel A13= ("Observations") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A14= ("R-squared") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A15= ("Cluster Count") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

putexcel A17= ("Industry-Year Sample") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A18= ("Industry-Year FE") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify


reghdfe n_ltfpq_ASM_m2 n_linput n_ltfpq_ASM_m1, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "B"
	putexcel `col'3 = ("(1)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'4 = ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'5 = ("(Constant RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'6 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'9 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'13 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'17 = ("ALL") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'18 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
		
		
reghdfe n_ltfpq_ASM_m2 n_linput n_ltfpq_ASM_m1 if rts_m1<1, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "C"
	putexcel `col'3 = ("(2)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'4 = ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'5 = ("(Constant RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'6 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'9 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'13 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'17 = ("DECREASING RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'18 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

reghdfe n_ltfpq_ASM_m2 n_linput n_ltfpq_ASM_m1 if rts_m1>=1, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "D"
	putexcel `col'3 = ("(3)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'4 = ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'5 = ("(Constant RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'6 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'9 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'13 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'17 = ("INCREASING RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'18 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	
putexcel A20= ("Panel B: Imposing Common Markups Across Industries and Across Time") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

putexcel A24= ("Normalized log Value Added") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A27= ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A28= ("(Heterogeneous Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

putexcel A31= ("Observations") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A32= ("R-squared") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A33= ("Cluster Count") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

putexcel A35= ("Industry-Year Sample") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
putexcel A36= ("Industry-Year FE") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify


reghdfe n_ltfpq_ASM_m3 n_lva n_ltfpq_ASM_m2, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "B"
	putexcel `col'21 = ("(1)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'22 = ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'23 = ("(Common Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'24 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'27 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'31 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'35 = ("ALL") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'36 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	
reghdfe n_ltfpq_ASM_m3 n_lva n_ltfpq_ASM_m2 if sigma_m3>sigma_m2, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "C"
	putexcel `col'21 = ("(2)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'22 = ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'23 = ("(Common Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'24 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'27 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'31 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'35 = ("HIGHER MEASURED MARKUP") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'36 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify

reghdfe n_ltfpq_ASM_m3 n_lva n_ltfpq_ASM_m2 if sigma_m3<=sigma_m2, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "D"
	putexcel `col'21 = ("(3)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'22 = ("Normalized log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'23 = ("(Common Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'24 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'27 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'31 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'35 = ("LOWER MEASURED MARKUP") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	putexcel `col'36 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4") modify
	
/****************************************
Establishment Regressions: LOG Variables
*****************************************/
putexcel A1= ("Table 4: Productivity Mismeasurement at the Establishment Level") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A2= ("Panel A: Imposing Constant Returns to Scale") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

putexcel A6= ("Log Input Bundle") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A9= ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A10= ("(Variable RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

putexcel A13= ("Observations") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A14= ("R-squared") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A15= ("Cluster Count") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

putexcel A17= ("Industry-Year Sample") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A18= ("Industry-Year FE") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify


reghdfe ltfpq_ASM_m2 linput ltfpq_ASM_m1, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "B"
	putexcel `col'3 = ("(1)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'4 = ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'5 = ("(Constant RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'6 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'9 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'13 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'17 = ("ALL") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'18 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
		
		
reghdfe ltfpq_ASM_m2 linput ltfpq_ASM_m1 if rts_m1<1, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "C"
	putexcel `col'3 = ("(2)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'4 = ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'5 = ("(Constant RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'6 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'9 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'13 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'17 = ("DECREASING RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'18 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

reghdfe ltfpq_ASM_m2 linput ltfpq_ASM_m1 if rts_m1>=1, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "D"
	putexcel `col'3 = ("(3)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'4 = ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'5 = ("(Constant RTS)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'6 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'9 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'13 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'17 = ("INCREASING RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'18 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	
putexcel A20= ("Panel B: Imposing Common Markups Across Industries and Across Time") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

putexcel A24= ("Log Input Bundle") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A27= ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A28= ("(Heterogeneous Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

putexcel A31= ("Observations") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A32= ("R-squared") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A33= ("Cluster Count") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

putexcel A35= ("Industry-Year Sample") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
putexcel A36= ("Industry-Year FE") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify


reghdfe ltfpq_ASM_m3 lva n_ltfpq_ASM_m2, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "B"
	putexcel `col'21 = ("(1)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'22 = ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'23 = ("(Common Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'24 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'27 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'31 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'35 = ("ALL") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'36 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	
reghdfe ltfpq_ASM_m3 lva ltfpq_ASM_m2 if sigma_m3>sigma_m2, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "C"
	putexcel `col'21 = ("(2)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'22 = ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'23 = ("(Common Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'24 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'27 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'31 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'35 = ("HIGHER MEASURED MARKUP") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'36 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify

reghdfe ltfpq_ASM_m3 lva ltfpq_ASM_m2 if sigma_m3<=sigma_m2, abs(cells) vce(clus cells)
	matrix REG = r(table)
	svmat REG, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(REG) nomiss
	drop reg*
		matrix OUT1 = REG[1..3,1]
		matrix OUT2 = REG[1..3,2]
		
	matrix SUM = [e(N) \ e(r2_a)\ e(N_clust)]
	svmat SUM, n(reg)
	foreach var of varlist reg* {
		tostring `var', replace force
		gen temp = strpos(`var',".")
			replace temp = . if temp==0
		replace temp = temp+4
		replace `var' = substr(`var',1,temp)
		destring `var', replace
		drop temp
	}
	mkmat reg*, mat(SUM) nomiss
	drop reg*
	
	local col "D"
	putexcel `col'21 = ("(3)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'22 = ("Log TFPQ") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'23 = ("(Common Markups)") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'24 = matrix(OUT1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'27 = matrix(OUT2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'31 = matrix(SUM) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'35 = ("LOWER MEASURED MARKUP") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify
	putexcel `col'36 = ("YES") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 4 log") modify



}

/*********************************************************
SECTION 5: 
	- Compare Sectoral Misallocation and Correlations
		
**********************************************************/
if `S5' == 1 {

/*Baseline + Robustness: CMF Sample*/
use "`data'Misallocation_Industry_CM_vrts_1acf_sest_5y.dta", clear
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_sest_5y.dta"
		drop _m
	merge 1:1 fk_naics02 year using "`data'Misallocation_Industry_CM_crts_1acf_s3_5y.dta"
		drop _m

/*
/*Net Counterfactuals*/
foreach var of varlist M* {
	replace `var' = `var'-1
	}
*/
	
rename M_ASM_CM_vrts_1acf_sest_5y M_ASM_m1
rename M_ASM_CM_crts_1acf_sest_5y M_ASM_m2
rename M_ASM_CM_crts_1acf_s3_5y M_ASM_m3

rename M_i_ASM_CM_vrts_1acf_sest_5y M_i_ASM_m1
rename M_i_ASM_CM_crts_1acf_sest_5y M_i_ASM_m2
rename M_i_ASM_CM_crts_1acf_s3_5y M_i_ASM_m3

keep fk year *_m*

merge 1:1 fk year using "`data'Misallocation_Correlations_industry.dta"
	drop _m

merge 1:1 fk year using "`data'VA_Shares_fk.dta"
	drop _m
		
gen DM_31 = M_ASM_m3/M_ASM_m1
gen DM_32 = M_ASM_m3/M_ASM_m2
gen DM_21 = M_ASM_m2/M_ASM_m1

gen overstate_mu = 0
	replace overstate_mu = 1 if sigma_m3/(sigma_m3-1)>=sigma_m2/(sigma_m2-1)

gen rts1 = alphaK_m1+alphaL_m1
gen IRTS = 0
	replace IRTS = 1 if rts1>=1
	
egen types = group(overstate_mu IRTS)
	
/*Decomposing DM32*/
	bys year overstate_mu: egen temp = sum(va_fk)
	bys year: egen temp_total = sum(va_fk)
	gen share = va_fk/temp
	gen share_overstate = temp/temp_total
	
	gen temp_m2 = share*log(M_i_ASM_m2)
	gen temp_m3 = share*log(M_i_ASM_m3)
	by year overstate_mu: egen temp2 = sum(temp_m2)
	by year overstate_mu: egen temp3 = sum(temp_m3)
	gen temp2_decomp =exp(temp2)
	gen temp3_decomp =exp(temp3)
	
	gen DM_32_decomp = (temp3_decomp/temp2_decomp)^share_overstate
	
	/*Correlations by Overstate*/
	bys year overstate: egen rho_m2_overstate = sum(rho_m2*share)
	bys year overstate: egen rho_m3_overstate = sum(rho_m3*share)
	drop temp* share share_overstate
	
	gen drho_32 = rho_m3_overstate-rho_m2_overstate
	
/*Decomposing DM21*/
	bys year IRTS: egen temp = sum(va_fk)
	bys year: egen temp_total = sum(va_fk)
	gen share = va_fk/temp
	gen share_IRTS = temp/temp_total
	
	gen temp_m1 = share*log(M_i_ASM_m1)
	gen temp_m2 = share*log(M_i_ASM_m2)
	by year IRTS: egen temp1 = sum(temp_m1)
	by year IRTS: egen temp2 = sum(temp_m2)
	gen temp1_decomp =exp(temp1)
	gen temp2_decomp =exp(temp2)
	
	gen DM_21_decomp = (temp2_decomp/temp1_decomp)^share_IRTS
	
	/*Correlations by Overstate*/
	bys year IRTS: egen rho_m1_IRTS = sum(rho_m1*share)
	bys year IRTS: egen rho_m2_IRTS = sum(rho_m2*share)
	drop temp* share share_IRTS
	
	gen drho_21 = rho_m2_IRTS-rho_m1_IRTS
	
	foreach var of varlist DM_* {
		gen l`var' = log(`var')
		}

	gen temp = lDM_32_decomp if overstate_mu==1
	bys year: egen lDM_32_over = mean(temp)
	drop temp
	
	gen temp = lDM_32_decomp if overstate_mu==0
	bys year: egen lDM_32_under = mean(temp)
	drop temp

	gen temp = lDM_21_decomp if IRTS==1
	bys year: egen lDM_21_irts = mean(temp)
	drop temp
	
	gen temp = lDM_21_decomp if IRTS==0
	bys year: egen lDM_21_drts = mean(temp)
	drop temp
	
	gen temp = rho_m3_overstate if overstate==1
	bys year: egen rho_m3_over = mean(temp)
	drop temp
	
	gen temp = rho_m3_overstate if overstate==0
	bys year: egen rho_m3_under = mean(temp)
	drop temp
	
	gen temp = rho_m2_overstate if overstate==1
	bys year: egen rho_m2_over = mean(temp)
	drop temp
	
	gen temp = rho_m2_overstate if overstate==0
	bys year: egen rho_m2_under = mean(temp)
	drop temp
	
	gen temp = rho_m2_IRTS if IRTS==1
	bys year: egen rho_m2_irts = mean(temp)
	drop temp
	
	gen temp = rho_m2_IRTS if IRTS==0
	bys year: egen rho_m2_drts = mean(temp)
	drop temp
	
	gen temp = rho_m1_IRTS if IRTS==1
	bys year: egen rho_m1_irts = mean(temp)
	drop temp
	
	gen temp = rho_m1_IRTS if IRTS==0
	bys year: egen rho_m1_drts = mean(temp)
	drop temp
	
	gen temp = drho_32 if overstate==1
	bys year: egen drho_32_over = mean(temp)
	drop temp
	
	gen temp = drho_32 if overstate==0
	bys year: egen drho_32_under = mean(temp)
	drop temp
	
	gen temp = drho_21 if IRTS==1
	bys year: egen drho_21_irts = mean(temp)
	drop temp
	
	gen temp = drho_21 if IRTS==0
	bys year: egen drho_21_drts = mean(temp)
	drop temp
	
egen ty = tag(year)
*br year lDM_31 lDM_32 lDM_21 lDM_32_over lDM_32_under lDM_21_irts lDM_21_drts if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
*br year lDM_31 lDM_32 lDM_21 lDM_32_over lDM_32_under lDM_21_irts lDM_21_drts drho_32_over drho_32_under drho_21_irts drho_21_drts if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)

keep if ty==1 & (year==1982 | year==1987 | year == 1992 | year == 1997 | year == 2002 | year == 2002 | year==2007)
keep year lDM_31 lDM_32 lDM_21 lDM_32_over lDM_32_under lDM_21_irts lDM_21_drts drho_32_over drho_32_under drho_21_irts drho_21_drts

foreach var of varlist lDM_31 lDM_32 lDM_21 lDM_32_over lDM_32_under lDM_21_irts lDM_21_drts drho_32_over drho_32_under drho_21_irts drho_21_drts {
	tostring `var', replace force
	gen temp = strpos(`var',".")
		replace temp = . if temp==0
	replace temp = temp+4
	replace `var' = substr(`var',1,temp)
	destring `var', replace
	drop temp
	}

putexcel A1= ("Table 5: Decomposing the Bias in Measured Misallocation") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel A3= ("Panel A: Misallocation Bias") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify

putexcel A4= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel B4= ("Aggregate Bias") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel C4= ("Constant RTS Bias") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel D4= ("Markup Bias") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel E4= ("CRTS Bias: Increasing RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel F4= ("CRTS Bias: Decreasing RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel G4= ("Markup Bias: Overstate") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel H4= ("Markup Bias: Understate") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify

putexcel A13= ("Panel B: Change in Correlation of Productivity and Distortions") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify

putexcel A14= ("Year") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel E14= ("CRTS Bias: Increasing RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel F14= ("CRTS Bias: Decreasing RTS") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel G14= ("Markup Bias: Overstate") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel H14= ("Markup Bias: Understate") using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify


mkmat year lDM_31 lDM_21 lDM_32 lDM_21_irts lDM_21_drts lDM_32_over lDM_32_under, mat(m5_a)
putexcel A5 = matrix(m5_a) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify

mkmat year, mat(m5_b1)
mkmat drho_21_irts drho_21_drts drho_32_over drho_32_under, mat(m5_b2)
putexcel A15 = matrix(m5_b1) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify
putexcel E15 = matrix(m5_b2) using "`output'Misallocation_Results_Aggregate.xls", sheet("Table 5") modify






}

/*End of file*/
